package inputDemo;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

//FileNotFoundException 是IOException 的子类
public class Demo1 {
    public static void main(String[] args) throws IOException {
        //打开一个文件 如果文件不存在 则会抛出异常
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream("d:/cat.jpg");
            while (true) {
//            int i = inputStream.read();
//            if(i == -1){
//                //读取完毕
//                break;
//            }
//            System.out.printf("ox%X ", i);
//            System.out.printf("ox%x ", i);

                byte[] bytes = new byte[1024];
                int n = inputStream.read(bytes);
                System.out.println("n = " + n);
                if (n == -1) {
                    //读取完毕。n 就是 -1
                    break;
                }
                for (int i = 0; i < n; i++) {
                    System.out.printf("ox%x ", bytes[i]);
                }
                System.out.println();
            }
        }finally {
            inputStream.close();
        }
    }
}
